<?php
namespace App\Core;


use App\Core\Configure\PhpConfig;

class App
{
    private static $instance = null;

    /**
     * @var bool 是否是调试模式
     */
    private $_debugMode = false;

    /**
     * @var ConfigureInterface
     */
    private $_configEngine;
    /**
     * @var Request
     */
    protected $_request;
    /**
     * @var Response
     */
    protected $_response;

    private function __construct()
    {
        $this->setConfigEngine(new PhpConfig());
    }

    /**
     * @return App
     */
    public static function &getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 执行HTTP请求
     */
    public function execute()
    {
        $dispatcher = new Dispatcher($this);
        $dispatcher->dispatch();
    }

    /**
     *
     * @param ConfigureInterface $configEngine
     */
    public function setConfigEngine(ConfigureInterface $configEngine)
    {
        $this->_configEngine = $configEngine;
    }

    public function setRequest($requestClass)
    {
        $this->_request = new $requestClass($this);
    }

    /**
     * @return Request
     */
    public function getRequest()
    {
        if (!$this->_request) {
            $this->_request = new Request($this);
        }
        return $this->_request;
    }

    public function setResponse($responseClass)
    {
        $this->_response = new $responseClass($this);
    }

    public function getResponse()
    {
        if (!$this->_response) {
            $this->_response = new Response($this);
        }
        return $this->_response;
    }

    /**
     * @return ConfigureInterface
     */
    public function getConfigEngine()
    {
        return $this->_configEngine;
    }

    /**
     * 获取系统配置
     *
     * @param string $name
     * @param string $file
     *
     * @return mixed
     */
    public function getConfig($name, $file = 'common')
    {
        return $this->_configEngine->get($name, $file);
    }


    /**
     * 检查是否开启调试模式
     *
     * @return bool
     */
    public function isDebugEnabled()
    {
        return $this->_debugMode;
    }

    /**
     * 开启或关闭 debug 模式
     *
     * @param bool|true $state
     */
    public function openDebug($state = true)
    {
        $this->_debugMode = $state;
    }

    /**
     * @var array 视图文件路径
     */
    private $_viewPaths = [];

    public function getViewPaths()
    {
        return $this->_viewPaths;
    }

    public function setViewPaths($viewPaths)
    {
        $this->_viewPaths = $viewPaths;
    }
}